package cn.laixueit.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class LoginJspFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        int indexOf = uri.lastIndexOf("/");
        String urlName = uri.substring(indexOf+1);
        List<String> jspNames = Arrays.asList("login.jsp", "signup.jsp");
        boolean b = jspNames.contains(urlName);
        if(b){
            chain.doFilter(request,response);
        }else{
            HttpServletResponse resp = (HttpServletResponse) response;
            String path = req.getContextPath();
            resp.sendRedirect(path+"/login.jsp");
        }
    }
}
